package com.zx.common.action;

import com.google.common.collect.Lists;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;

// 获得gitee上某个人的全部git地址
@Slf4j
public class GiteeGitUrls {

    public static void main(final String[] args) throws Exception {
        final String domain = "https://gitee.com/";
        final String uri = "/zx19890628/projects";

        final CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建httpclient实例
        final HttpGet httpGet = new HttpGet(domain + uri); // 创建httpget实例
        httpGet.setHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102");

        final CloseableHttpResponse response = httpclient.execute(httpGet); // 执行get请求
        final HttpEntity entity = response.getEntity(); // 获取返回实体
        final String content = EntityUtils.toString(entity, "utf-8");
        final Document doc = Jsoup.parse(content); // 解析网页 得到文档对象

        // 分页的页数
        final Elements linkElements = doc.select("#git-discover-page a"); //通过选择器查找所有博客链接DOM
        final List<String> pageUrls = Lists.newArrayList();
        for (final Element e : linkElements) {
            //https://gitee.com/zx19890628/projects?page=2
            if (!StringUtils.isEmpty(e.text())) {
                pageUrls.add(domain + uri + "?page=" + e.text());
            }
        }
        // 打开每一页，获得项目的地址
        final List<String> projectUtls = Lists.newArrayList();
        for (final String url : pageUrls) {
            final HttpGet get = new HttpGet(url); // 创建httpget实例
            get.setHeader("User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102");
            final CloseableHttpResponse execute = httpclient.execute(get);
            final HttpEntity httpEntity = execute.getEntity();
            final String text = EntityUtils.toString(httpEntity, "utf-8");
            final Document document = Jsoup.parse(text); // 解析网页 得到文档对象
            final Elements select = document.select("#search-projects-ulist .list-warpper");
            for (final Element e : select) {
                projectUtls.add(domain + "/" + e.attr("data-path"));
            }
        }
        // 分别打开项目地址
        final List<String> gitUrls = Lists.newArrayList();
        for (final String url : projectUtls) {
            final HttpGet get = new HttpGet(url); // 创建httpget实例
            get.setHeader("User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102");
            final CloseableHttpResponse execute = httpclient.execute(get);
            final HttpEntity httpEntity = execute.getEntity();
            final String text = EntityUtils.toString(httpEntity, "utf-8");
            final Document document = Jsoup.parse(text); // 解析网页 得到文档对象
            final Elements select = document.select("#project_clone_url");//
            for (final Element e : select) {
                gitUrls.add(e.attr("value"));
            }
        }
        log.info("domain={}, uri={}, gitUrl={}个", domain, uri, gitUrls.size());
        for (final String git : gitUrls) {
            System.out.println(git);
        }

        response.close();
        httpclient.close();
    }

}
